package com.obrock;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;

/**
 * Created with IntelliJ IDEA.
 * User: Дмитрий Г. Кузнецов
 * Date: 19.10.12
 * Time: 14:24
 * To change this template use File | Settings | File Templates.
 */
public class Settings extends PreferenceActivity {
    SharedPreferences prefs = Main.context.getSharedPreferences(Main.PREFERENCES_NAME, 0);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.settings);


        CheckBoxPreference syncEnable = (CheckBoxPreference) findPreference("sync_enable");
        syncEnable.setChecked(prefs.getBoolean("SYNC_ENABLE", false));
        syncEnable.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(Preference preference, Object o) {

                SharedPreferences prefs = getApplicationContext().getSharedPreferences(Main.PREFERENCES_NAME, 0);
                SharedPreferences.Editor edit = prefs.edit();
                edit.putBoolean("SYNC_ENABLE", (Boolean) o);
                edit.commit();


                if ((Boolean) o) {
                    DropboxThread.enable();
                    DropboxThread.force();
                } else {
                    DropboxThread.disable();
                }

                    /*
                    Main.db.setParam("SYNC_ENABLE", 1);
                } else {
                    Main.db.setParam("SYNC_ENABLE", 0);
                }
                */
                return true;
            }
        });

        EditTextPreference syncPeriod = (EditTextPreference) findPreference("sync_period");
        syncPeriod.setText(String.format("%d", prefs.getLong("SYNC_PERIOD", 600000L) / 60000L));
        syncPeriod.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(Preference preference, Object o) {
                try {
                    //Main.db.setParam("SYNC_PERIOD", Long.parseLong((String) o) * 60000);

                    SharedPreferences prefs = getApplicationContext().getSharedPreferences(Main.PREFERENCES_NAME, 0);
                    SharedPreferences.Editor edit = prefs.edit();
                    edit.putLong("SYNC_PERIOD", Long.parseLong((String) o) * 60000L);
                    edit.commit();

                    //getApplicationContext().getSharedPreferences(Main.PREFERENCES_NAME, 0).edit().putLong("SYNC_PERIOD", Long.parseLong((String) o) * 60000L);
                    //getApplicationContext().getSharedPreferences(Main.PREFERENCES_NAME, 0).edit().commit();
                } catch (Exception e) {
                }
                return true;
            }
        });

        CheckBoxPreference smsEnable = (CheckBoxPreference) findPreference("sms_enable");
        smsEnable.setChecked(prefs.getBoolean("SMS_ENABLE", false));
        smsEnable.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(Preference preference, Object o) {

                SharedPreferences prefs = getApplicationContext().getSharedPreferences(Main.PREFERENCES_NAME, 0);
                SharedPreferences.Editor edit = prefs.edit();
                edit.putBoolean("SMS_ENABLE", (Boolean) o);
                edit.commit();

                if ((Boolean) o) {
                    MessageThread.enable();
                    MessageThread.force();
                } else {
                    MessageThread.disable();
                }

                //getApplicationContext().getSharedPreferences(Main.PREFERENCES_NAME, 0).edit().putBoolean("SMS_ENABLE", (Boolean) o);
                //getApplicationContext().getSharedPreferences(Main.PREFERENCES_NAME, 0).edit().commit();
                /*
                if ((Boolean) o) {
                    Main.db.setParam("SMS_ENABLE", 1);
                } else {
                    Main.db.setParam("SMS_ENABLE", 0);
                }
                */
                return true;
            }
        });

        Preference templates = (Preference) findPreference("templates");
        templates.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                Main.context.setMode(Main.VIEW_TEMPLATES);
                finish();
                return true;
            }
        });

        Preference templatesExport = (Preference) findPreference("templates_export");
        templatesExport.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                Main.context.doMenuAction(R.id.templates_export);
                //finish();
                return true;
            }
        });

        Preference templatesImport = (Preference) findPreference("templates_import");
        templatesImport.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                Main.context.doMenuAction(R.id.templates_import);
                //finish();
                return true;
            }
        });


        Preference syncNow = (Preference) findPreference("sync_now");
        syncNow.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                Main.context.doMenuAction(R.id.sync);
                finish();
                return true;
            }
        });

        Preference setAuth = (Preference) findPreference("set_auth");
        setAuth.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                Main.context.doMenuAction(R.id.set_auth);
                finish();
                return true;
            }
        });


        Preference removeAuth = (Preference) findPreference("remove_auth");
        removeAuth.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                Main.db.unsetParam("login");
                Main.db.unsetParam("password");
                Main.toast(getResources().getString(R.string.msg_rmlogin));
                return true;
            }
        });

        EditTextPreference afterCount = (EditTextPreference) findPreference("after_count");
        afterCount.setText(String.format("%d", prefs.getInt("PROGNOSIS_DAYS_AFTER", 10)));
        afterCount.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(Preference preference, Object o) {
                try {

                    SharedPreferences prefs = getApplicationContext().getSharedPreferences(Main.PREFERENCES_NAME, 0);
                    SharedPreferences.Editor edit = prefs.edit();
                    edit.putLong("PROGNOSIS_DAYS_AFTER", Integer.parseInt((String) o));
                    edit.commit();

                    //Main.db.setParam("SYNC_PERIOD", Long.parseLong((String) o) * 60000);
                    //getApplicationContext().getSharedPreferences(Main.PREFERENCES_NAME, 0).edit().putInt("PROGNOSIS_DAYS_AFTER", Integer.parseInt((String) o));
                    //getApplicationContext().getSharedPreferences(Main.PREFERENCES_NAME, 0).edit().commit();
                } catch (Exception e) {
                }
                return true;
            }
        });

        Preference optimize = (Preference) findPreference("optimize_database");
        optimize.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                //Dropbox.dataReset(); //.force();
                Main.context.doMenuAction(R.id.data_reset);
                finish();
                return true;
            }
        });

        /*
        Preference collapse = (Preference) findPreference("collapse_transfers");
        collapse.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                //Dropbox.dataReset(); //.force();
                //Main.toast("Not ready yet");
                Main.context.doMenuAction(R.id.data_collapse);
                finish();
                return true;
            }
        });
        */

    }
}
